/*
 * Copyright 2012 Jonathan St-Michel <jonathan@abiwebz.net>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.abiwebz.office.rm.mysql.driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import net.abiwebz.office.rm.database.DatabaseDriver;
import net.abiwebz.office.rm.database.configuration.DriverConfigurationPanel;
import net.abiwebz.office.rm.mysql.driver.panel.MySQLDriverConfigurationPanel;
import org.openide.util.Exceptions;

/**
 *
 * @author Jonathan St-Michel <jonathan@abiwebz.net>
 */
public class MySQLDriver extends DatabaseDriver {
    public static final String MYSQL_DRIVER_ID = "ABIWEBZ_MYSQL_DRIVER";

    public MySQLDriver() {
        super("com.mysql.jdbc.Driver");
    }

    @Override
    protected void register(String classname) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class.forName(classname);
        //System.out.println("Mysql Driver registered!");
    }

    @Override
    public Connection createConnection(String host, String database, String port, String username, char[] password) throws SQLException {
        this.register();
        return DriverManager.getConnection(buildURL(host, database, port), username, new String(password));
    }

    @Override
    protected String getDisplayName() {
        return "Mysql JDBC Driver";
    }

    @Override
    public DriverConfigurationPanel getConfigurationPanel(DatabaseDriver driver) {
        return new MySQLDriverConfigurationPanel(driver);
    }

    @Override
    public String[] getDatabases(Connection con) {
        ArrayList<String> databases = new ArrayList<String>();
        try {
            ResultSet rs = con.prepareStatement("SHOW DATABASES").executeQuery();
            while(rs.next()){
                databases.add(rs.getString(1));
            }
        } catch (SQLException ex) {
            Exceptions.printStackTrace(ex);
        }
        
        String[] dblist = new String[databases.size()];
        for(int i = 0; i < databases.size(); i++)
            dblist[i] = databases.get(i);
        return dblist;
    }

    @Override
    public String getDriverID() {
        return MYSQL_DRIVER_ID;
    }

    @Override
    public String buildURL(String host, String database, String port) {
        return "jdbc:mysql://" + host + ":" + port + "/" + database;
    }

    @Override
    public Connection createConnection(String url, String username, char[] password) throws SQLException {
        this.register();
        return DriverManager.getConnection(url, username, new String(password));
    }

    @Override
    public Connection createConnection(String url) throws SQLException {
        this.register();
        return DriverManager.getConnection(url, "jsm", "K0uK0um1");
    }

    @Override
    protected String[] getAdministrators() {
        return new String[]{"root"};
    }

    @Override
    public String getErrorMessage(int code) {
        String message = "";
        switch(code){
            case 1045: message = "Access denied for user!";
                break;
            default: message = "Unknow error!";
                break;
        }
        return message;
    }
    
}
